﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SafeScript.MetaModel.Statements
{
    public class AsOperator : Operator
    {
        public ITypeDef Type;
        public Operator Operator;

        public override int GetPrecedence()
        {
            return 75;
        }

        public override ITypeDef ComputeType(ITypeDef goal, bool needsRef, ErrorReport report)
        {
            ITypeDef t = this.Operator.ComputeType(null, needsRef, report);
            if (t == null)
                return null;
            if (!(t is InstanceType))
            {
                report.Add(new ErrorEntry(this.Node) { Type = ErrorType.NotAnObject });
                return null;
            }
            return new InstanceType() { Type = this.Type };
        }
    }

    public class IsOperator : Operator
    {
        public ITypeDef Type;
        public Operator Operator;

        public override int GetPrecedence()
        {
            return 75;
        }

        public override ITypeDef ComputeType(ITypeDef goal, bool needsRef, ErrorReport report)
        {
            ITypeDef t = this.Operator.ComputeType(null, needsRef, report);
            if (t == null)
                return null;
            if (!(t is InstanceType))
            {
                report.Add(new ErrorEntry(this.Node) { Type = ErrorType.NotAnObject });
                return null;
            }
            return new InstanceType() { Type = BoolType.Self };
        }
    }
}
